﻿using Microsoft.Data.Sqlite;
using System.Data.SQLite;

namespace ClassLibrary_fqy_NET.SQLiteHelper
{
    /// <summary>
    /// 数据库连接池
    /// </summary>
    public class SQLiteConnectionPool
    {
        private static readonly object _lockObject = new object();
        private static readonly Queue<SQLiteConnection> _systemConnectionPool = new Queue<SQLiteConnection>();
        private static readonly Queue<SqliteConnection> _microsoftConnectionPool = new Queue<SqliteConnection>();
        private static int _systemConnectionPoolSize;
        private static int _microsoftConnectionPoolSize;

        /// <summary>
        /// 初始化连接池
        /// </summary>
        /// <param name="poolSize">连接池大小</param>
        /// <param name="connectionString">数据库连接字符串</param>
        public static void InitializeSystemConnectionPool(int poolSize, string connectionString)
        {
            _systemConnectionPoolSize = poolSize;
            for (int i = 0; i < poolSize; i++)
            {
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                _systemConnectionPool.Enqueue(connection);
            }
        }
        /// <summary>
        /// 初始化连接池
        /// </summary>
        /// <param name="poolSize">连接池大小</param>
        /// <param name="connectionString">数据库连接字符串</param>
        public static void InitializeMicrosoftConnectionPool(int poolSize, string connectionString)
        {
            _microsoftConnectionPoolSize = poolSize;
            for (int i = 0; i < poolSize; i++)
            {
                var connection = new SqliteConnection(connectionString);
                connection.Open();
                _microsoftConnectionPool.Enqueue(connection);
            }
        }


        /// <summary>
        /// 从连接池中获取连接
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetSystemConnection()
        {
            lock (_lockObject)
            {
                return _systemConnectionPool.Dequeue();
            }
        }
        /// <summary>
        /// 从连接池中获取连接
        /// </summary>
        /// <returns></returns>
        public static SqliteConnection GetMicrosoftConnection()
        {
            lock (_lockObject)
            {
                return _microsoftConnectionPool.Dequeue();
            }
        }
        /// <summary>
        /// 将连接送回连接池或直接关闭连接
        /// </summary>
        /// <param name="connection">数据库连接字符串</param>
        public static void ReturnSystemConnection(SQLiteConnection connection)
        {
            if (connection.State == System.Data.ConnectionState.Open)
            {
                lock (_lockObject)
                {
                    if (_systemConnectionPool.Count < _systemConnectionPoolSize)
                    {
                        _systemConnectionPool.Enqueue(connection);
                    }
                    else
                    {
                        connection.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 将连接送回连接池或直接关闭连接
        /// </summary>
        /// <param name="connection">数据库连接字符串</param>
        public static void ReturnMicrosoftConnection(SqliteConnection connection)
        {
            if (connection.State == System.Data.ConnectionState.Open)
            {
                lock (_lockObject)
                {
                    if (_microsoftConnectionPool.Count < _microsoftConnectionPoolSize)
                    {
                        _microsoftConnectionPool.Enqueue(connection);
                    }
                    else
                    {
                        connection.Close();
                    }
                }
            }
        }

    }
}
